package club.wx3609.images.controller;

import club.wx3609.images.Utils.IsNullUtils;
import club.wx3609.images.entity.User;
import club.wx3609.images.interfaces.Constant;
import club.wx3609.images.service.UserService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * 用户管理控制器
 */
@Controller
public class UserController {

    @Resource
    private UserService userService;

    private Integer pageSize=10;
    private String sortTitle="userId";

    private String[] registerStateList={"注册中","已注册","已禁用"};

    @GetMapping("/MiYiTuKu/Management/Users/{putPageNumber}")
    public String ImagesManage(@PathVariable("putPageNumber") Integer putPageNumber, Map<String,Object> map, HttpServletRequest request){
        Object object=request.getSession().getAttribute("User");
        if(object!=null) {
            User user=(User)object;
            Page<User> page=userService.findAllUser(putPageNumber,pageSize,sortTitle);
            List<User> Users=userService.findAll();
            Integer pageNumbers=0;
            int num=(Users.size())%pageSize;
            pageNumbers=Integer.valueOf((Users.size())/pageSize);
            if(num>0){
                pageNumbers++;
            }
            if(pageNumbers>0){
                pageNumbers--;
            }
            map.put("pageNumbers",pageNumbers);
            map.put("pageNumber",putPageNumber);
            map.put("page",page);
            map.put("User",user);
            map.put("registerStates",registerStateList);
            return "UsersManage";
        }
        return "error";
    }

    @RequestMapping("/MiYiTuKu/Management/Users/selectUser")
    public String selectUser(String userNumber,String userName,String registerState,Map<String,Object> map,HttpServletRequest request){

        return "UsersManage";
    }

    @Transactional
    @ResponseBody
    @PostMapping("/MiYiTuKu/Management/Users/operationUserUser")
    public String deleteUser(String userId,String operationUser){
        if(IsNullUtils.character(userId)&&IsNullUtils.character(operationUser)){
            User user=userService.registerVerification(userId);
            if(user!=null){
                switch (operationUser){
                    case "add":
                        user.setRegisterState(Constant.registerState1);
                        userService.save(user);
                        return "同意注册";
                    case "prohibit":
                        if(!user.getRegisterState().equals(Constant.registerState3)) {
                            user.setRegisterState(Constant.registerState3);
                            userService.save(user);
                            return "账号禁用成功";
                        }else{
                            user.setRegisterState(Constant.registerState1);
                            userService.save(user);
                            return "账号解除禁用";
                        }
                    case "delete":
                        userService.delete(user);
                        return "用户删除成功";
                }
            }
            return Constant.addFriendErrorState0;
        }
        return Constant.inputJudge0;
    }

}
